﻿Imports System.Data.SqlClient
Imports System.Web.Configuration

Public Class Progress
    Inherits System.Web.UI.Page

    ''' <summary>
    ''' Set the database connection variables used throughout this class.
    ''' </summary>
    ''' <remarks>Placed here so they will not need to be typed out each time they are needed.</remarks>
    Private connectionString As String = WebConfigurationManager.ConnectionStrings("SqlConnectionString").ConnectionString()
    Private connection As SqlConnection = New SqlConnection(connectionString)
    Private command As New SqlCommand

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' Create a dataset containing the information on current progress.
        GetProgressDataSet()
    End Sub

    Private Sub GetProgressDataSet()
        command = New SqlCommand("sp_public_getProgression", connection)
        command.CommandType = CommandType.StoredProcedure
        Dim da As New SqlDataAdapter(command)
        Dim ds As New DataSet()
        da.Fill(ds)

        ds.Relations.Add(New DataRelation("Game2Realm", ds.Tables(0).Columns("Game_Id"), ds.Tables(1).Columns("Realm_Game")))
        ds.Relations.Add(New DataRelation("Realm2Raid", ds.Tables(1).Columns("Realm_Id"), ds.Tables(2).Columns("Raid_Realm")))
        ds.Relations.Add(New DataRelation("Raid2Boss", ds.Tables(2).Columns("Raid_Id"), ds.Tables(3).Columns("Boss_Raid")))

        repGames.DataSource = ds.Tables(0)
        repGames.DataBind()
    End Sub

#Region " Item DataBinding for the list of current characters. "

    ''' <summary>
    ''' Databind the repGames control.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.Web.UI.WebControls.RepeaterItemEventArgs" /> instance containing the event data.</param>
    Protected Sub repGames_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
        If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim drv As DataRowView = TryCast(e.Item.DataItem, DataRowView)
            Dim repRealms As Repeater = TryCast(e.Item.FindControl("repRealms"), Repeater)
            repRealms.DataSource = drv.CreateChildView("Game2Realm")
            repRealms.DataBind()
        End If
    End Sub

    ''' <summary>
    ''' Databinds the repRealms control.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.Web.UI.WebControls.RepeaterItemEventArgs" /> instance containing the event data.</param>
    Protected Sub repRealms_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
        If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim drv As DataRowView = TryCast(e.Item.DataItem, DataRowView)
            Dim repRaids As Repeater = TryCast(e.Item.FindControl("repRaids"), Repeater)
            repRaids.DataSource = drv.CreateChildView("Realm2Raid")
            repRaids.DataBind()
        End If
    End Sub

    ''' <summary>
    ''' Databind the repRaids control.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.Web.UI.WebControls.RepeaterItemEventArgs" /> instance containing the event data.</param>
    Protected Sub repRaids_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
        If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim drv As DataRowView = TryCast(e.Item.DataItem, DataRowView)
            Dim repBosses As Repeater = TryCast(e.Item.FindControl("repBosses"), Repeater)
            repBosses.DataSource = drv.CreateChildView("Raid2Boss")
            repBosses.DataBind()
        End If
    End Sub

#End Region

End Class